package net.lsr.demo.helper;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * desc: 日期帮助类
 * @author lsr
 * @version 2014年5月14日
 */
public class DateHelper {
    private static final Logger logger = LoggerFactory.getLogger(DateHelper.class);
    
	public static Date getBeginOfDay(Date oneday){
		Calendar c = Calendar.getInstance();
		c.setTime(oneday);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}
	
	public static Date getEndOfDay(Date oneday){
		Calendar c = Calendar.getInstance();
		c.setTime(oneday);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}
	
	public static int getOneDayNumber(Date oneday){
		Calendar c = Calendar.getInstance();
		c.setTime(oneday);
		return c.get(Calendar.DAY_OF_MONTH);
	}
	
   public static Date getOneDay(String oneday, String pattern){
        DateFormat df = new SimpleDateFormat(pattern);
        Date day = null;
        try {
            day = df.parse(oneday);
        } catch (ParseException e) {
            logger.debug("getOneDay parse date exception:{}", e.getMessage());
        }
        return day;
    }
	
	public static Date getBeginOfMonth(int year, int month){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}
	
	public static Date getEndOfMonth(int year, int month){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}
	
	public static int getLastDayOfMonth(int year, int month){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		return c.get(Calendar.DAY_OF_MONTH);
	}
	
	public static Date getDate(long timestamp){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(timestamp);
		return c.getTime();
	}
	
	public static Date getDate(int year, int month, int day){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, day);
		return c.getTime();
	}
	
   public static String getDay(int year, int month, int day){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);
        Date date = c.getTime();
        return getDate(date, "yyyy-MM-dd");
    }

	public static String getDay(Date date){
		return getDate(date, "yyyy-MM-dd");
	}
	
	public static String getDate(Date date, String pattern){
		DateFormat df = new SimpleDateFormat(pattern);
		return df.format(date);
	}
	
	
	public static List<String> getDaysOfMonth(int year, int month){
		List<String> days = new ArrayList<String>();
		int lastDay = getLastDayOfMonth(year, month);
		for(int day = 1; day <= lastDay; day++){
			Date d = getDate(year, month, day);
			days.add(getDay(d));
		}
		return days;
	}
	
	/**
	 * 获取某月的有效日子，及最大是当前日期
	 * @param year
	 * @param month
	 * @return
	 */
	public static List<String> getValidDaysOfMonth(int year, int month){
		List<String> days = new ArrayList<String>();
		int lastDay = getLastDayOfMonth(year, month);
		if(isCurrentMonth(year, month))
			lastDay = getOneDayNumber(new Date());
		for(int day = 1; day <= lastDay; day++){
			Date d = getDate(year, month, day);
			days.add(getDay(d));
		}
		return days;
	}
	
	public static String getDutyTimeOfDay(Date someday, int someHour, int someMinute, String pattern){
		Calendar c = Calendar.getInstance();
		c.setTime(someday);
		c.set(Calendar.HOUR_OF_DAY, someHour);
		c.set(Calendar.MINUTE, someMinute);
		return getDate(c.getTime(), pattern);
	}
	
	public static int getCurrentYear(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return c.get(Calendar.YEAR);
	}
	
	public static int getCurrentMonth(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return c.get(Calendar.MONTH);
	}
	
	public static Date getNow(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return c.getTime();
	}
	
	public static String getNow(String pattern){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return getDate(c.getTimeInMillis(), pattern);
	}
	
	public static String getDate(long timestamp, String pattern){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(timestamp);
		
		return getDate(c.getTime(), pattern);
	}
	
	public static boolean isCurrentMonth(int year, int month){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(new Date().getTime());
		
		return c.get(Calendar.YEAR) == year && (c.get(Calendar.MONTH)+1) == month;
	}
	
	/**
	 * 获取某天是星期
	 * @return
	 */
	public static int getDayOfWeek(Date oneday){
	    Calendar c = Calendar.getInstance();
	    c.setTime(oneday);
	    // 星期日-星期六
	    return c.get(Calendar.DAY_OF_WEEK);
	}
	
}
